//random array
function randomArray() {
    var arr = [];
    for (var i = 0; i < 5; i++) {//生成一个1-6的数组
        arr[i] = i+1;
    }
    for(var i = arr.length - 1; i; i--){  
        var temp, random = parseInt(Math.random() * i);  
            temp = arr[i];  
            arr[i] = arr[random];  
            arr[random] = temp;  
          
    }  
    var arrayNum = new Array();
    for( index in arr ){
    	var num = arr[index];
    	if( num == 1){
			arrayNum.push("一");
		}else if ( num == 2 ){
			arrayNum.push("二");
		}else if ( num == 3 ){
			arrayNum.push("三");
		}else if ( num == 4 ){
			arrayNum.push("四");
		}else if ( num == 5 ){
			arrayNum.push("五");
		}
    }
    return arrayNum;
}

// 班组排班规则随机生成
function groupShow( ){
	$("#searchCondition").show();
	$("#searchCondition2").hide();
	$("#searchCondition3").hide();
	$("#calendar").show().html("");
	$("#personCalendar").hide().html("");
	$("#yearCalendar").hide();
	
	var date = new Date();
	var d = date.getDate();
	var m = date.getMonth();
	var y = date.getFullYear();
	var eventsArray = new Array();
	
	for(var i = 1; i < 31; i ++ ){
			var randArray = randomArray();
			eventsArray.push({
				title: '白班：' + randArray[0] + '班',
				start: new Date(y, m, i),
				color: '#018ED6'
				
			});
			eventsArray.push({
				title: '中班：' + randArray[1] + '班',
				start: new Date(y, m, i),
				color: '#DCB86A'
			});
		
			eventsArray.push({
				title: '夜班：' + randArray[2] + '班',
				start: new Date(y, m, i),
				color: '#352D2B'
			});
			
			eventsArray.push({
				title: '休息：' + randArray[3] + '班',
				start: new Date(y, m, i),
				color: '#82C247'
			});
			eventsArray.push({
				title: '休息：' + randArray[4] + '班',
				start: new Date(y, m, i),
				color: '#82C247'
			});
		
			
	}
	
	//渲染日历
	$('#calendar').fullCalendar({
		header:{
              right: 'prev,next today',
              center: 'title',
              left: 'prevYear,nextYear'
        },
        buttonText :{
        	 prev: '上月',
        	 next: '下月',
        	 prevYear: '去年',
        	 nextYear: '明年',
        	 today : '今天'
        },
        titleFormat : {
        	month: 'yyyy年MM月'
        },
        height : '600',
        dayNames : ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
        dayNamesShort : ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
		editable: false,
		events : eventsArray
	});
}


//个人视图
function personShow(){
	$("#calendar").hide();
	$("#searchCondition").hide();
	$("#searchCondition3").hide();
	$("#personCalendar").show().html("");
	$("#searchCondition2").show();
	$("#yearCalendar").hide();
	
	
	var date = new Date();
	var d = date.getDate();
	var m = date.getMonth();
	var y = date.getFullYear();
	var eventsArray = new Array();
	
	for(var i = 0; i < 30; i ++ ){
		if( i % 9 == 0 ){
			eventsArray.push({
				title: '中班',
				start: new Date(y, m, i+1),
				color: '#DCB86A'
			});
		
			eventsArray.push({
				title: '休息',
				start: new Date(y, m, i+2),
				color: '#82C247'
			});
			eventsArray.push({
				title: '休息',
				start: new Date(y, m, i+3),
				color: '#82C247'
			});
			eventsArray.push({
				title: '白班',
				start: new Date(y, m, i+4),
				color: '#018ED6'
				
			});
			eventsArray.push({
				title: '白班',
				start: new Date(y, m, i+5),
				color: '#018ED6'
			});
			eventsArray.push({
				title: '休息',
				start: new Date(y, m, i+6),
				color: '#82C247'
			});
			eventsArray.push({
				title: '休息',
				start: new Date(y, m, i+7),
				color: '#82C247'
			});
			eventsArray.push({
				title: '夜班',
				start: new Date(y, m, i+8),
				color: '#352D2B'
			});
			eventsArray.push({
				title: '夜班',
				start: new Date(y, m, i+9),
				color: '#352D2B'
			});
			
		}
	}
	
	
	$('#personCalendar').fullCalendar({
		header:{
              right: 'prev,next today',
              center: 'title',
              left: 'prevYear,nextYear'
        },
        buttonText :{
        	 prev: '上月',
        	 next: '下月',
        	 prevYear: '去年',
        	 nextYear: '明年',
        	 today : '今天'
        },
        titleFormat : {
        	month: 'yyyy年MM月'
        },
        height : '500',
        dayNames : ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
        dayNamesShort : ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
		editable: false,
		events : eventsArray
	});
}

//控制年视图的显示及其他视图的隐藏
function yearShow(){
	$("#calendar").hide();
	$("#personCalendar").hide();
	$("#searchCondition").hide();
	$("#searchCondition2").hide();
	$("#searchCondition3").show();	
	$("#yearCalendar").show();
	
}

function createYearSchedule(flag){	
//创建全年的日历空格子
	var i, j;
	var html;
	for (j = 0; j < 10; j++) {
		var temp1 = j + 1;
		html = html + "<tr style='height:28px;' id="+flag+"_"+temp1+">";
		for (i = 0; i < 24; i++) {
			var temp2 = i + 1;
			html += "<td style='min-width:14px;border:1px solid;border-color:#DDDDDD;' id="+flag+"_"+temp1+"_"+temp2+"></td>";
		}
		html += "</tr>"
	}

	$("#"+flag+" tbody").append(html);

}

function fillIn(sR,sC,sM,flag)
 {
	/* 根据设定的起始位置，将2014年填入格子内
	 * 格子ID：flag_row_col
	*/
		var rowIndex = sR;
		var colIndex = sC;
		var month, day;
		var year = 2014;
		var today=new Date();
		for (month = sM; month < sM+6; month++) {
			var monthMaxDay = getLastDay(year, month);
			for (day = 1; day < monthMaxDay + 1; day++) {
				var currentDate = new Date(year, month - 1, day);
				var fillId="#"+flag+"_" + rowIndex + "_" + colIndex;
				//console.log(fillId);
				if (today.getMonth() == currentDate.getMonth()&&
					today.getDate()== currentDate.getDate()) {
					$(fillId).css("background-color","yellow");
				}
				$(fillId).html(day);
				if (currentDate.getDay() == 0) {
					$(fillId).css("color", "red");
				}
				if (currentDate.getDay() == 6) {
					$(fillId).css("color", "green");
				}
				rowIndex++;
				if (rowIndex > 10) {
					rowIndex = 1;//超过一个周期就从第一行开始
					colIndex++;//同时列数+1
				}
			}//月内循环结束
			
			if (rowIndex != 1) {
				colIndex++;
			}
			if (colIndex | 4 != 1) {
				colIndex = month * 4 + 1;
			}
			if(colIndex>24){
				colIndex-=24;
			}
		}
		if(flag=="first"){
		fillIn(rowIndex, 1, month, "second");
		}
 }
function createZhibie(id,fieldWidth)
{
	var testData = [ 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}, 
	                {"a" : "白","b" : "中","c" : "学","d" : "夜","e" : "休"}
	                ];
	var columns=[
	          {field:'a',title:'A<br/>值',width:fieldWidth,fixed:true,resizable:false},
	          {field:'b',title:'B<br/>值',width:fieldWidth,fixed:true,resizable:false},
	          {field:'c',title:'C<br/>值',width:fieldWidth,fixed:true,resizable:false},
	          {field:'d',title:'D<br/>值',width:fieldWidth,fixed:true,resizable:false},
	          {field:'e',title:'E<br/>值',width:fieldWidth,fixed:true,resizable:false}
	      ];
	//创建最左边的值别周期表
	$("#"+id).datagrid({
			"pagination" : false,
			"fitColumns" : true,
			"singleSelect" : true,
			"data" : testData,
			"scrollbarSize" : 0,
			"autoRowHeight":true,
			"columns":[columns]
		});

}

//获得当前月份的最大天数
function getLastDay(p_year, p_month) {
	var new_year = p_year; // 取当前的年份
	var new_month = p_month++;// 取下一个月的第一天，方便计算（最后一天不固定）
	if (p_month > 12) // 如果当前大于12月，则年份转到下一年
	{
		new_month -= 12; // 月份减
		new_year++; // 年份增
	}
	var new_date = new Date(new_year, new_month, 1); // 取当年当月中的第一天
	return (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate();//获取当月最后一天日期        
}